#include <sys/inotify.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>


#include "../../common/include/filesystem.h"
#include "../../common/include/general.h"
#include "../../common/include/list.h"


#define EVENT_SIZE  (sizeof (struct inotify_event))
#define BUF_LEN_NOTIF   ((EVENT_SIZE + MAX_DIR_LEN))*128
//#define IN_ISDIR                0x40000000
//#define IN_ACCESS               0x00000001      /* File was accessed */
//#define IN_MODIFY               0x00000002      /* File was modified */
//#define IN_ATTRIB               0x00000004      /* Metadata changed */
//#define IN_CLOSE_WRITE          0x00000008      /* Writtable file was closed */
//#define IN_CLOSE_NOWRITE        0x00000010      /* Unwrittable file closed */
//#define IN_OPEN                 0x00000020      /* File was opened */
//#define IN_MOVED_FROM           0x00000040      /* File was moved from X */
//#define IN_MOVED_TO             0x00000080      /* File was moved to Y */
//#define IN_CREATE               0x00000100      /* Subfile was created */
//#define IN_DELETE               0x00000200      /* Subfile was deleted */

#define MASK_EVENTS (IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_IGNORED)
//#define MASK_EVENTS IN_ALL_EVENTS
/*
            IN_ACCESS         File was accessed (read) (*).
            IN_ATTRIB         Metadata changed, e.g.,  permissions,  timestamps,  extended  attributes,  link
                             count (since Linux 2.6.25), UID, GID, etc. (*).
            IN_CLOSE_WRITE    File opened for writing was closed (*).
            IN_CLOSE_NOWRITE  File not opened for writing was closed (*).
            IN_CREATE         File/directory created in watched directory (*).
            IN_DELETE         File/directory deleted from watched directory (*).
            IN_DELETE_SELF    Watched file/directory was itself deleted.
            IN_MODIFY         File was modified (*).
            IN_MOVE_SELF      Watched file/directory was itself moved.
            IN_MOVED_FROM     File moved out of watched directory (*).
            IN_MOVED_TO       File moved into watched directory (*).
            IN_OPEN           File was opened (*).

*/
typedef struct watchD
{
    int fd;
    listADT waList;
}watchD;

typedef struct watchPath
{
    int wd;
    char* path;
}watchPath;

int update(watchD *wd, struct inotify_event* event);
int remWatch(watchD *wd, char* path);
int addWatchs(watchD* wd, char* pathRoot);
char *getWatchCompletePath(struct inotify_event *e, watchD *w);
int setWatchs(char* path, watchD* wd);

/*
funcion setWatchs que recorre todo el path y sus subdirectorios
y aniade los watch point para cada directorio. mantiene un registro
de path de dir con sus respectivos watch descriptors.
devuelve 0 si se setea correctamente, -1 error.
*/

int notify(void);
/*
funcion notify devuelve el file descriptor iniciado por inotify_init, ademas
crea una lista de todos los watchpoints y sus respectivos pathslo y se guarda
en una estructura watchD
*/
